package com.cloudptt.wp.ui.message;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.ProductConst;
import com.cloudptt.api.product.func.call.group.fsm.GroupCallConferenceListener;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.AddrOperator;
import com.cloudptt.api.product.operator.MsgOperator;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ScreenManager;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.message.adapter.GroupMemberAdapter;
import com.cloudptt.wp.ui.message.adapter.MessageStatusAdapter;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.GroupMembInfo;
import com.mcptt.db.model.SessionReadStatus;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class GroupMemberActivity extends Activity implements View.OnClickListener, GroupCallConferenceListener {

    public static final String KEY_SHOW = "show";
    private GroupMemberAdapter mAdapter;
    private List<GroupMembInfo> membList = null;
    private ListView memberLv;
    private AddrInfo groupInfo = null;
    private boolean isShowStatus = false;
    private boolean isAddMemb = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(R.color.app_theme_color_primary));

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read_status);
        TextView tv = (TextView) findViewById(R.id.title);
        tv.setText(R.string.title_group_member);
        ImageView iv = (ImageView) findViewById(R.id.back_up_icon);
        iv.setOnClickListener(this);

        groupInfo =(AddrInfo)getIntent().getSerializableExtra(SessionActivity.KEY);
        if(groupInfo != null
            &&(groupInfo.isEmergencyNum()
            || groupInfo.isCall210()))
        {
            isAddMemb = true;
        }
        isShowStatus = true;// getIntent().getBooleanExtra(KEY_SHOW,false);
        Log.debug(this.getClass(),"onCreate sessionInfo =  "+ groupInfo + " isShowStatus = " + isShowStatus);

        memberLv = (ListView) findViewById(R.id.memberLv);

        SysApplication app = ((SysApplication) getApplicationContext());
        Product product = app.getProduct();
        AddrOperator op = (AddrOperator)product.getOperator(ProductConst.Func_Addr);
        String selfAddrId = product.getSysConfig().getMcInfo().getAddrId();
        membList = op.getGroupMemberList(groupInfo,selfAddrId);
        Log.debug(this.getClass()," onCreate membList = " + membList);
        mAdapter = new GroupMemberAdapter(GroupMemberActivity.this, isShowStatus,membList);
        memberLv.setAdapter(mAdapter);
        memberLv.requestFocus();
        memberLv.setSelection(0);
        product.getGCallEventAdapter().subcribeConference();

        ScreenManager.getScreenManager( ).pushActivity( this );
    }

    @Override
    protected void onResume() {
        super.onResume();
        SysApplication app = ((SysApplication) getApplicationContext());
        Product product = app.getProduct();
        product.setGCallConferenceListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.back_up_icon)
        {
            this.finish();
            overridePendingTransition( R.anim.in_from_left, R.anim.out_to_right );
        }
    }

    @Override
    public void updateConferenceInfoList(String groupAddrId, boolean isFull,Map<String, Boolean> conferenceInfoMap) {
        Log.debug(this.getClass(),"updateConferenceInfoList groupAddrId =  "+ groupAddrId +" isFull = "+isFull +  " conferenceInfoMap = " + conferenceInfoMap);
        if(isAddMemb
            && conferenceInfoMap != null
            && conferenceInfoMap.size() > 0)
        {
            Set<String> keySet = conferenceInfoMap.keySet();
            for(Iterator<String> itor = keySet.iterator();itor.hasNext();)
            {
                String num = itor.next();
                groupAddrId = groupInfo.getAddrId();
                String numUrl = AddrInfo.toUrlFromNum(num);
                GroupMembInfo info = new GroupMembInfo(groupAddrId,num,num,numUrl,0);
                if(!isHas(info))
                {
                    membList.add(info);
                }
            }
        }
        if(mAdapter != null)
        {
            mAdapter = new GroupMemberAdapter(GroupMemberActivity.this, isShowStatus,membList);
            memberLv.setAdapter(mAdapter);
            memberLv.requestFocus();
            memberLv.setSelection(0);
            mAdapter.refresh(isFull,conferenceInfoMap);
            mAdapter.notifyDataSetChanged();
        }
    }
    private boolean isHas(GroupMembInfo info )
    {
        Log.debug(this.getClass(),"isHas info =  "+ info +" membList = " + membList);
        for (Iterator<GroupMembInfo> itor = membList.iterator(); itor.hasNext();)
        {
            GroupMembInfo membInfo = itor.next();
            String addrId = info.getAddrId();
            if(membInfo != null
                && membInfo.isSameOne(addrId))
            {
                return true;
            }
        }
        return false;
    }

}
